package cn.tude.egcar1.controller;

import cn.tude.egcar1.base.response.JsonResult;
import cn.tude.egcar1.base.response.StatusCode;
import cn.tude.egcar1.mapper.UserMapper;
import cn.tude.egcar1.pojo.dto.UserLoginParam;
import cn.tude.egcar1.pojo.dto.UserRegParam;
import cn.tude.egcar1.pojo.entity.User;
import cn.tude.egcar1.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RequestMapping("/v1/user")
@RestController
@Tag(name = "用户管理模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("reg")
    @Operation(summary="用户注册")
    @ApiOperationSupport(order=10)
    public JsonResult reg (@RequestBody UserRegParam userRegParam){
        log.debug("userRegParam"+ userRegParam);
        String username=userRegParam.getUsername();
        int i=userMapper.selectByUsername(username);
        if(i>0){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        String password=userRegParam.getPassword();
        int p=userMapper.selectByPassword(password);
        if(userRegParam.getPassword().length()<6||userRegParam.getPassword().length()>12){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);}

            User user=new User();
            BeanUtils.copyProperties(userRegParam, user);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            userMapper.insertUser(user);
            return  JsonResult.ok();
        }
        @PostMapping("login")
    @Operation(summary="用户登录")
    @ApiOperationSupport(order=20)
    public JsonResult longin(@RequestBody UserLoginParam userLoginParam){
        log.debug("userLoginParam="+userLoginParam);
        UserVO userVO=userMapper.selectByUsernameAndPassword(userLoginParam);
        if(userVO!=null){
            return JsonResult.ok();


        }
        return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
    }


    }


